package com.zpark.controller;

import com.zpark.entity.User;
import com.zpark.service.IUserService;
import com.zpark.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    private static final Integer STATUS_CODE = 20000;

    private static final String SUCCESS = "OK";

    private static final String ERROR = "ERROR";


    // 新增数据
    @RequestMapping("/add")
    public JsonUtil<Integer> addUser(User user) {
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.addUser(user));
    }

    // 删除数据
    @RequestMapping("/del")
    public JsonUtil<Integer> delUserById(Integer id){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.delUserById(id));
    }

    // 修改数据
    @RequestMapping("/update")
    public JsonUtil<Integer> changeUserUnameById(User user){
        return new JsonUtil<>(STATUS_CODE, SUCCESS,userService.changeUser(user));
    }

    // 查询所有用户数据
    @RequestMapping("/all")
    public JsonUtil<List<User>>  findAll(){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.findAll());
    }

    // 根据ID查询用户
    @RequestMapping("/find_id")
    public JsonUtil<User> findById(Integer id){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.findById(id));
    }

    @RequestMapping("/find_like/{name}")
    public JsonUtil<List<User>>  findLike(@PathVariable("name") String uname){
        return new JsonUtil<>(STATUS_CODE, SUCCESS, userService.findByUnameLike(uname));
    }
}